﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using BrainTechLLC;
using BrainTechLLC.WPF;
using ImageAppViewModels;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for ImageControl.xaml
    /// </summary>
    public partial class ImageControl : UserControl
    {
        public FileImageViewModel _vm;

        public static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageControl), new PropertyMetadata(null, OnImageSourcePropertyChanged));

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(ImageControl), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { _vm = value; SetValue(FileImageViewModelProperty, value); }
        }
        
        public ImageSource ImageSource
        {
            get { return (ImageSource)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        private static void OnImageSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageControl source = d as ImageControl;
            source.UpdateImageSource(e.OldValue as ImageSource, e.NewValue as ImageSource);
        }

        private void UpdateImageSource(ImageSource viewModelOld, ImageSource viewModelNew)
        {            
            ImageSource = viewModelNew;
            imgMain.Source = ImageSource;            
        }
        
        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageControl source = d as ImageControl;
            source.UpdateFileImageViewModel(e.NewValue as FileImageViewModel, e.OldValue as FileImageViewModel);
        }

        private void UpdateFileImageViewModel(FileImageViewModel viewModel, FileImageViewModel viewModelOld)
        {
            if (viewModelOld != null)
            {
                viewModelOld.PropertyChanged -= viewModel_PropertyChanged;
            }

            FileImageViewModel = viewModel;

            if (viewModel != null)
            {
                viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModel_PropertyChanged);

                // kick off any necessary image source preloading/processing
                ImageSource src = viewModel.GetImageSource();

                if (src != null)
                {
                    ImageSource = src;
                }
            }
        }

        private void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            FileImageViewModel vm = sender as FileImageViewModel;

            if (vm == _vm && e.PropertyName == "ImageSource")
            {
                Dispatcher.RunOnDispatcher(new VoidDelegate(() =>
                {
                    ImageSource src = FileImageViewModel.Item.ImageSourceCached;

                    if (src == null)
                    {
                        // Console.WriteLine("Image source on " + FileImageViewModel.FileNameOnly + " set to null");
                    }

                    if (src != ImageSource)
                    {
                        ImageSource = src;
                    }
                }),
                DispatcherPriority.Send);
            }
        }

        public ImageControl()
        {
            InitializeComponent();
        }
    }
}
